package com.fly.estore.service.Impl;

import com.fly.estore.dao.MemberPointDAO;
import com.fly.estore.dao.SaleDAO;
import com.fly.estore.dao.SaleDetailDAO;
import com.fly.estore.dao.StockDetailDAO;
import com.fly.estore.domain.MemberPoint;
import com.fly.estore.domain.SaleDetail;
import com.fly.estore.domain.SaleInfo;
import com.fly.estore.domain.StockDetail;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.service.SaleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Fly on 2016/4/28.
 */
@Service("SaleService")
public class SaleServiceImpl implements SaleService {

    @Autowired
    SaleDAO saleDAO;
    @Autowired
    SaleDetailDAO saleDetailDAO;
    @Autowired
    StockDetailDAO stockDetailDAO;
    @Autowired
    MemberPointDAO memberPointDAO;

    public int saveSale(SaleInfo saleInfo) {
        int update = 0;
        if (StringUtils.isNotBlank(saleInfo.getSaleId())){
            saleInfo.setSaleId(CommonUtil.genPK());
            update = saleDAO.addSaleInfo(saleInfo);
        }
        else{
            update = saleDAO.updateSaleInfo(saleInfo);
        }
        return update;
    }

    public int saveSale(SaleInfo saleInfo, String saleId) {
        int update = 0;
        if (StringUtils.isBlank(saleInfo.getSaleId())){
            saleInfo.setSaleId(saleId);
            saleInfo.setStatus(SaleInfo.STATUS_NOT_CONFIRM);
            update = saleDAO.addSaleInfo(saleInfo);
        }
        else{
            update = saleDAO.updateSaleInfo(saleInfo);
        }
        return update;
    }

    public int confirmSale(String saleId, float cutrate) {
        SaleInfo sale = saleDAO.getById(saleId);
        sale.setCutrate(cutrate);
        sale.setPayPrice(sale.getTotalPrice() - sale.getCutrate());
        sale.setStatus(SaleInfo.STATUS_CONFIRMED);
        int update = saleDAO.updateSaleInfo(sale);
        if (update > 0){
            //减库存
            List<SaleDetail> detailList = saleDetailDAO.getBySaleId(saleId);
            for(SaleDetail detail : detailList){
                StockDetail stockDetail = new StockDetail();
                stockDetail.setStockDetailId(CommonUtil.genPK());
                stockDetail.setProduct(detail.getProduct());
                stockDetail.setProductSKU(detail.getProductSKU());
                stockDetail.setAmount(-1 * detail.getAmount());
                stockDetail.setCreateId(sale.getCreateId());
                stockDetail.setCreateTime(CommonUtil.currentTime());
                stockDetail.setLastUpdateId(sale.getLastUpdateId());
                stockDetail.setLastUpdateTime(CommonUtil.currentTime());
                stockDetailDAO.addStockDetail(stockDetail);
            }
            //增加会员积分
            MemberPoint memberPoint = new MemberPoint();
            memberPoint.setMemberPointId(CommonUtil.genPK());
            memberPoint.setMemberId(sale.getMember().getMemberId());
            memberPoint.setPoint((int)sale.getPayPrice() / 10);
            memberPoint.setDescription("购买商品，金额：" + sale.getPayPrice());
            memberPoint.setCreateId(sale.getCreateId());
            memberPoint.setCreateTime(CommonUtil.currentTime());
            memberPointDAO.addMemberPoint(memberPoint);
        }
        return update;
    }

    public int updateSalePrice(String saleId) {
        int update = 0;
        List<SaleDetail> detailList = saleDetailDAO.getBySaleId(saleId);
        float price = 0f;
        for(SaleDetail detail: detailList){
            price = price + detail.getPrice();
        }

        SaleInfo sale = saleDAO.getById(saleId);
        if (null != sale){
            sale.setTotalPrice(price);
            update = saleDAO.updateSaleInfo(sale);
        }
        return update;
    }

    public int deleteSaleById(String saleId) {
        int update = 0;
        update = saleDAO.deleteById(saleId);
        if (update> 0){
            saleDetailDAO.deleteBySaleId(saleId);
        }
        return update;
    }

    public SaleInfo getSaleById(String saleId) {
        SaleInfo sale = saleDAO.getById(saleId);
        if (null != sale){
            sale.setDetailList(saleDetailDAO.getBySaleId(saleId));
        }
        return sale;
    }

    public List<SaleInfo> getSaleList() {
        return saleDAO.getList();
    }

    public int saveSaleDetail(SaleDetail saleDetail) {
        int update = 0;
        if (StringUtils.isBlank(saleDetail.getSaleDetailId())){
            saleDetail.setSaleDetailId(CommonUtil.genPK());
            update = saleDetailDAO.addSaleDetail(saleDetail);
        }
        else{
            update = saleDetailDAO.updateSaleDetail(saleDetail);
        }
        return update;
    }

    public float getSalePrice(String date) {
        return saleDAO.getSalePrice(date);
    }

    public int deleteDetailById(String saleDetailId) {
        return saleDetailDAO.deleteById(saleDetailId);
    }

    public SaleDetail getDetailById(String saleDetailId) {
        return saleDetailDAO.getById(saleDetailId);
    }

    public List<SaleInfo> getSaleListByMemberId(String memberId) {
        return saleDAO.getListByMemberId(memberId);
    }

    public List querySalePrice(String date) {
        return null;
    }
}
